﻿package util.color {
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;
	
	public class ColorPicker extends Sprite {
		private var editor:Editor;
		
		private var colorPane:ColorPane;
		private var colorSquare:ColorSquare;
		private var input:TextField;
		
		private var currentColor:uint; //当前所选颜色
		
		public function ColorPicker(editor:Editor) {
			super();
			visible = false;
			this.editor = editor;
			//初始化
			initPane();
			initSquare();
			initView();
			//侦听
			addEventListener(MouseEvent.MOUSE_MOVE, onMoveHandler);
			addEventListener(MouseEvent.MOUSE_UP, onUpHandler);
		}
		
		private function initPane():void {
			colorPane = new ColorPane();
			colorPane.x = 6.5;
			colorPane.y = 5.5;
			addChild(colorPane);
		}
		private function initSquare():void {
			colorSquare = new ColorSquare();
			addChild(colorSquare);
		}
		private function initView():void {
			input = getChildByName("txt") as TextField;
			input.restrict = "#A-Fa-f0-9";
			input.maxChars = 7;
			input.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
		}
		
		private function onMoveHandler(event:MouseEvent):void {
			var mx:int = event.localX - 6 , my:int = event.localY -30 ;
			//在颜色选择区域内
			if (isInSelectArea(mx, my)) {
				mx = mx + 6 - mx % 11;
				my = my + 30 - my % 11;
				//如果移动到新的颜色方块上则更新，否则不需要
				if (colorSquare.isAtPosition(mx, my) == false) {
					//移动并显示
					colorSquare.moveTo(mx, my);
					colorSquare.visible = true;
					//计算出当前位置的16进制颜色
					var bitMapData:BitmapData = new BitmapData(width, height, true, 0xFFFFFF);
					bitMapData.draw(this);
					currentColor = bitMapData.getPixel(mx + 2, my + 2);
					colorPane.setColor(currentColor);
					setColorText();
				}
			}
		}
		private function onUpHandler(event:MouseEvent):void {
			var mx:int = event.localX - 6 , my:int = event.localY -30 ;
			//在颜色选择区域内，进行设置
			if (isInSelectArea(mx, my)) {
				editor.hideColorPicker(currentColor);
			}
		}
		private function onKeyUpHandler(event:KeyboardEvent):void {
			//阻止回车事件
			event.preventDefault();
			//回车输入颜色
			if (event.keyCode == 13) {
				//正则验证合法
				var reg:RegExp = /^\#?((?:[\da-f]{6})|(?:[\da-f]{3}))/i;
				var res:Array;
				if ((res = reg.exec(input.text)) != null) {
					var color:Array = new Array();
					//当使用缩写时
					if (res[1].length == 3) {
						for (var i:int = 0; i < 3; i++) {
							color.push(res[1].charAt(i));
							color.push(res[1].charAt(i));
						}
					}
					else if(res[1].length == 6) {
						color.push(res[1]);
					}
					else {
						return;
					}
					currentColor = parseInt(color.join(""), 16);
					colorPane.setColor(currentColor);
					editor.hideColorPicker(currentColor);
				}
			}
		}
		
		private function isInSelectArea(mx:int, my:int):Boolean {
			return (mx > 0  && mx < 220 && my > 0 && my < 132);
		}
		private function setColorText():void {
			var color:String = currentColor.toString(16);
			var length:int = color.length;
			//补全可能因转换丢失的头字符串
			if (length < 6) {
				for (var i:int = 0; i < 6 - length; i++) {
					color = "0" + color;
				}
			}
			//设置text
			input.text = "#" + color;
		}
		
		public function show(x:int, y:int):void {
			this.x = x;
			this.y = y;
			visible = true;
		}
	}
	
}